﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Linq.Expressions;

namespace CommonSense.UI.MVVM.ViewModels
{
    public abstract class NotifyPropertyChangedBaseModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        protected virtual void OnPropertyChanged<T>(Expression<Func<T>> property)
        {
            MemberExpression memberExpression = property.Body as MemberExpression;
            if (memberExpression != null)
            {
                this.OnPropertyChanged(memberExpression.Member.Name);
            }
            else
            {
                throw new NotSupportedException("This type of expression is not supported");    
            }
        }
    }
}
